package org.cloud.ext.util;

import com.alibaba.fastjson.JSONObject;
import io.jsonwebtoken.*;
import lombok.extern.slf4j.Slf4j;
import org.cloud.ext.config.Audience;
import org.springframework.util.DigestUtils;

import javax.xml.bind.DatatypeConverter;
import java.util.Date;
import java.util.UUID;

/**
 * @author xukk
 * @date 2020/3/11
 */
@Slf4j
public class JwtTokenUtil {
    public static Claims parseJWT(String jsonWebToken, String pass) {
        try {
            Claims claims = Jwts.parser()
                    .setSigningKey(DigestUtils.md5Digest(pass.getBytes()))
                    .parseClaimsJws(jsonWebToken).getBody();
            return claims;
        } catch (ExpiredJwtException eje) {
            log.error("===== Token过期 =====", eje);
        } catch (Exception e){
            log.error("===== token解析异常 =====", e);
        }
        return null;
    }
    public static String createJWT(Long ttlMillis, Audience audience) {
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);
        if (ttlMillis == null) {
            ttlMillis = Long.valueOf(audience.getExpiresSecond()*1000);
        }
        long expMillis = nowMillis + ttlMillis;
        Date expDate = new Date(expMillis);
//        SecretKey secretKey = generalKey();

        JwtBuilder builder = Jwts.builder()
                //唯一的ID
                .setId(UUID.randomUUID().toString())
                // 主题  可以是JSON数据
                .setSubject(new JSONObject().toJSONString())
                // 签发者
                .setIssuer("admin")
                // 签发时间
                .setIssuedAt(now)
                //使用HS256对称加密算法签名, 第二个参数为秘钥
                .signWith(signatureAlgorithm, DigestUtils.md5Digest("Feihe".getBytes()))
                // 设置过期时间
                .setExpiration(expDate);
        return builder.compact();
    }
    public static void main(String[] args) {
        Audience audience=new Audience();
        audience.setExpiresSecond(172800);
        String a=createJWT(null,audience);
        System.out.println(a);
        //eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI1ZDIyNjU5MS02Y2E4LTRhNGUtOGIzZC1hOTE0NzFhOGI4MDUiLCJzdWIiOiJ7fSIsImlzcyI6ImFkbWluIiwiaWF0IjoxNTgzOTEyMzY3LCJleHAiOjE1ODQwODUxNjd9.S4x-w4RE8ZFyIXH-R4e0wGEvGj4KoUfocbizqujwdtM
       //Claims claims=parseJWT("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI3ZDMxNDA1My0zOWJkLTRjOWQtODhiOC0yOTMxOGI4YzcyYjciLCJzdWIiOiJ7XCJjcmVhdGVUaW1lXCI6MTU4MzA1NzI2MzAwMCxcImRlcHRJZFwiOjEwMjQsXCJlbWFpbFwiOlwibGl1bWFuZ0AxMjMuY29tXCIsXCJlbmFibGVzdGF0ZVwiOjEsXCJpZFwiOjIxMzYyLFwiaXNEZWxldGVcIjowLFwiaXNMZWFkZXJcIjowLFwiam9iTnVtXCI6XCJGSDYwMDEyOFwiLFwibG9naW5OYW1lXCI6XCJsaXVtYW5nXCIsXCJtb2JpbGVcIjpcIjE4MjM4NzA5NjAyXCIsXCJuYW1lXCI6XCLliJjoipJcIixcInBhc3N3b3JkXCI6XCJjbGtkUEhRVlNhQ2tIbm0wcHF0aG9nPT1cIixcInBob3RvVXJsXCI6XCJodHRwOi8vNDcuMTEwLjEyNy4xMDIvZmlsZXMvc3RhdGljL3VwbG9hZC9maWxlLzIwMjAwMzAzMTk0MzU0NzQ0LmpwZ1wiLFwicm9sZUlkc1wiOlszMCwzMV0sXCJzZXhcIjoxLFwic3RhdHVzXCI6MCxcInVwZGF0ZVRpbWVcIjoxNTgzNDA0MDg1MDAwLFwidXBkYXRlVXNlcklkXCI6MjEzNjJ9IiwiaXNzIjoiYWRtaW4iLCJpYXQiOjE1ODM5MDI5NzgsImV4cCI6MTU4NDUwNzc3OH0.-k6kTK47H9yPyetYcsyK-sHceMzUxXxWhmkAbFP4qN8"
        //,"FeiHe");
        Claims claims=parseJWT(a,"Feihe");
        System.out.println(1);

    }
}
